﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecipesManager.RecipesImporter
{
    class RecipeImporter
    {
      private static HashSet<string> keyWords;
      private static int index;
      private static ImportedRecipe recipe;
      private static string parsingMode;
      private static string[] lines;

      static RecipeImporter()
      {
        lines = null;
        index = 0;
        parsingMode = "waitForKeyWord";

        keyWords = new HashSet<string>();
        keyWords.Add("nome");
        keyWords.Add("originale");
        keyWords.Add("ingredienti");
        keyWords.Add("passi");
        keyWords.Add("tempo");
      }

      public static ImportedRecipe importRecipe(string filePath)
      {
        lines = System.IO.File.ReadAllLines(filePath);
          
        recipe = new ImportedRecipe();

        for (index = 0; index < lines.Length; index++)
        {
          if (isKeyWordLine())
            parsingMode = getCurrentKeyWord();
          else
            parsingMode = "waitForKeyWord";

          switch (parsingMode)
          {
            case "nome":
                        manageName();
                        break;
            case "originale":
                        manageOriginalSource();
                        break;
            case "ingredienti":
                        manageIngredients();
                        break;
            case "passi":
                        manageSteps();
                        break;
            default:    
                        break;
          }

        }
        return recipe;
      }

      private static string getCurrentKeyWord()
      {
        return lines[index].Trim().ToLower();
      }

      private static bool isKeyWordLine()
      {
        foreach (var k in keyWords)
        {
          if (lines[index].Trim().ToLower().Equals(k))
            return true;
        }

        return false;
      }

      private static void nextLine()
      {
        if (index + 1 <= lines.Length)
          index++;
      }

      private static void manageName()
      {
        nextLine();
        skipEmptyLines();
        recipe.Name = lines[index].Trim();
      }

      private static void manageOriginalSource()
      {
        nextLine();
        skipEmptyLines();
        recipe.OriginalSource = lines[index].Trim();
      }

      private static void manageIngredients()
      {
         
      }

      private static void manageSteps()
      {
        string allStepsInOneString = "";
        nextLine();
        skipEmptyLines();

        while (!isKeyWordLine())
        {
          allStepsInOneString = allStepsInOneString + lines[index].Trim().ToLower();
          
          if (isEndOfFile())
            break;

          nextLine();
          skipEmptyLines();
        }
        index--;//Altrimenti il main loop salta una keyword
        foreach (string s in allStepsInOneString.Split('.'))
        {
          if(!s.Equals(""))
            recipe.Steps.Add(s.Trim());
        }
        
      }

      private static void skipEmptyLines()
      {
        while(lines[index].Trim().Equals(""))
          nextLine();
      }

      private static bool isEndOfFile()
      {
        return index >= lines.Length - 1;
      }

      //Scrive il parametro sul file debugLog.txt. Da usare al posto del Console.write per il debugging
      private static void writeIntoDebugLog(string s)
      {
        System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Max\Documents\Visual Studio 2012\Projects\RecipeImporter\RecipeImporter\debugLog", true);
        file.WriteLine(s);
        file.Close();
      }
      

    }
}
